package com.ruoyi.system.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.system.domain.vo.WebsiteVo;
import com.ruoyi.system.service.ISysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.WebsiteMapper;
import com.ruoyi.system.domain.Website;
import com.ruoyi.system.service.IWebsiteService;

/**
 * 网址导航Service业务层处理
 * 
 * @author 何文财
 * @date 2021-09-14
 */
@Service
public class WebsiteServiceImpl implements IWebsiteService 
{
    @Autowired
    private WebsiteMapper websiteMapper;
    @Autowired
    private ISysDictDataService dictDataService;

    /**
     * 查询网址导航
     * 
     * @param id 网址导航ID
     * @return 网址导航
     */
    @Override
    public Website selectWebsiteById(Long id)
    {
        return websiteMapper.selectWebsiteById(id);
    }

    /**
     * 查询网址导航列表
     * 
     * @param website 网址导航
     * @return 网址导航
     */
    @Override
    public List<Website> selectWebsiteList(Website website)
    {
        return websiteMapper.selectWebsiteList(website);
    }

    /**
     * 新增网址导航
     * 
     * @param website 网址导航
     * @return 结果
     */
    @Override
    public int insertWebsite(Website website)
    {
        return websiteMapper.insertWebsite(website);
    }

    /**
     * 新增网址导航
     *
     * @param website 网址导航
     * @return 结果
     */
    @Override
    public int insertWebsite(WebsiteVo website)
    {
        Website web=new Website();
        web.setName(website.getName());
        if(!website.getUrl().contains("http://")&&!website.getUrl().contains("https://")){
            web.setUrl("http://"+website.getUrl());
        }else{
            web.setUrl(website.getUrl());
        }
        int res =websiteMapper.insertWebsite(web);
        List<Website> list= websiteMapper.selectWebsiteList(web);
        if(list.size()>0){
            web=list.get(0);
        }
        websiteMapper.insertWebsiteType(web.getId(),website.getType());
        return  res;
    }


    /**
     * 修改网址导航
     * 
     * @param website 网址导航
     * @return 结果
     */
    @Override
    public int updateWebsite(Website website)
    {
        return websiteMapper.updateWebsite(website);
    }

    /**
     * 批量删除网址导航
     * 
     * @param ids 需要删除的网址导航ID
     * @return 结果
     */
    @Override
    public int deleteWebsiteByIds(Long[] ids)
    {
        return websiteMapper.deleteWebsiteByIds(ids);
    }

    /**
     * 删除网址导航信息
     * 
     * @param id 网址导航ID
     * @return 结果
     */
    @Override
    public int deleteWebsiteById(Long id)
    {
        return websiteMapper.deleteWebsiteById(id);
    }

    /**
     * 分组查询全部网址
     * @return
     */
    @Override
    public Map<String,List<Website>> listGroupType() {
        SysDictData dictData=new SysDictData();
        dictData.setDictType("url_type");
        List<SysDictData> list = dictDataService.selectDictDataList(dictData);
        Map<String,List<Website>> map=new HashMap<>();
        list.forEach(sysDictData -> {
            List<Website> webs= websiteMapper.listGroupType(sysDictData.getDictLabel());
            map.put(sysDictData.getDictLabel(),webs);
        });
        return map;
    }


    @Override
    public List<Website> listByGroupName(String groupName) {
        return websiteMapper.listGroupType(groupName);
    }
}
